home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / awe2-0_1.lha / awe2-0.1 / Src / RCS / SharedMalloc.h,v < prev    next >
Text File  |  1988-09-18  |  3KB  |  121 lines

  1. head     1.1;
  2. access   ;
  3. symbols  ;
  4. locks    ; strict;
  5. comment  @ * @;
  6.  
  7.  
  8. 1.1
  9. date     88.09.18.16.42.15;  author grunwald;  state Exp;
  10. branches ;
  11. next     ;
  12.  
  13.  
  14. desc
  15. @@
  16.  
  17.  
  18.  
  19. 1.1
  20. log
  21. @Initial revision
  22. @
  23. text
  24. @#ifndef SharedMalloc_h
  25. #define SharedMalloc_h
  26.  
  27. char *malloc( unsigned );
  28. void free(char *);
  29. //  char *realloc(char *, unsigned);
  30. //
  31. //    calloc and cfree are defined in terms of malloc.
  32. //    alloca allocates space from the current stack.
  33. //
  34.  
  35. #include "assert.h"
  36. #include "HardSpinLock.h"
  37.  
  38. //
  39. // Allocator.h: based loosely on...
  40. //
  41. // malloc.c (Caltech) 2/21/82
  42. // Chris Kingsley, kingsley@@cit-20.
  43. //
  44. //    $Header: Allocator.h,v 1.5 88/09/16 20:51:37 russo Exp $
  45. //
  46. // 
  47.  
  48. //
  49. // Below is a allocator sbuclass based loosely on..
  50. //
  51. // malloc.c (Caltech) 2/21/82
  52. // Chris Kingsley, kingsley@@cit-20.
  53. //
  54. // This is a very fast storage allocator.  It allocates blocks of a small 
  55. // number of different sizes, and keeps free lists of each size. In this 
  56. // implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.
  57. // 
  58.  
  59. //
  60. // The overhead on a block is at least 4 bytes.  When free, this space
  61. // contains a pointer to the next free block, and the bottom two bits must
  62. // be zero.  When in use, the first byte is set to MAGIC, and the second
  63. // byte is the size index.  The remaining bytes are for alignment.
  64. // If range checking is enabled and the size of the block fits
  65. // in two bytes, then the top two bytes hold the size of the requested block
  66. // plus the range checking words, and the header word MINUS ONE.
  67. // 
  68. union    overhead {
  69.     union    overhead * ov_next;    // when free
  70.     struct {
  71.     unsigned char    ovu_magic;    // magic number
  72.     unsigned char    ovu_index;    // bucket #
  73.     unsigned short    ovu_size;    // actual block size
  74.     unsigned int    ovu_rmagic;    // range magic number
  75.     } ovu;
  76. };
  77.  
  78. #define    ov_magic    ovu.ovu_magic
  79. #define    ov_index    ovu.ovu_index
  80. #define    ov_size        ovu.ovu_size
  81. #define    ov_rmagic    ovu.ovu_rmagic
  82.  
  83. #define    MAGIC        0xff        /* magic # on accounting info */
  84. #define RMAGIC        0x55555555    /* magic # on range info */
  85. #define    RSLOP        sizeof( unsigned int )
  86.  
  87. //
  88. // nextf[i] is the pointer to the next free block of size 2^(i+3).  The
  89. // smallest allocatable block is 8 bytes.  The overhead information
  90. // precedes the data area returned to the user.
  91. // 
  92.  
  93. int const NumberOfBuckets = 30;
  94.  
  95. class BucketAllocator {
  96. protected:
  97.     union overhead * nextf[ NumberOfBuckets ];
  98.     HardSpinLock bucketLock[ NumberOfBuckets ];
  99.     
  100.     HardSpinLock topLock;
  101.     char * top;
  102.  
  103.     char sbrkDisabled;
  104.     char initialized;
  105.     
  106.     int morecore( int bucket );
  107.  
  108.     void initialize();
  109. public:
  110.     BucketAllocator();
  111.     ~BucketAllocator();
  112.     void * allocate( unsigned nbytes );
  113.     void free( void * cp );
  114.     void disableFurtherBreaks();
  115. };
  116.  
  117. extern BucketAllocator HardwareMemoryAllocator;
  118.  
  119. #endif /* SharedMalloc_h */
  120. @
  121.